<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="点位名称" prop="pointName">
        <el-input
          v-model="queryParams.pointName"
          placeholder="请输入点位名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="点位设备名称" prop="pointDeviceName">
        <el-input
          v-model="queryParams.pointDeviceName"
          placeholder="请输入点位设备名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="相位" prop="phase">
        <el-input
          v-model="queryParams.phase"
          placeholder="请输入相位"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="线路" prop="lineId">
        <el-select
          v-model="queryParams.lineId"
          placeholder="请选择线路"
          clearable
        >
          <el-option
            v-for="lineOne in lineList"
            :key="lineOne.id"
            :label="lineOne.lineName"
            :value="lineOne.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="电杆" prop="poleId">
        <el-select
          v-model="queryParams.poleId"
          placeholder="请选择电杆"
          clearable
        >
          <el-option
            v-for="poleOne in poleList"
            :key="poleOne.id"
            :label="poleOne.poleName"
            :value="poleOne.id"
          ></el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="设备温度" prop="deviceTemperature">
        <el-input
          v-model="queryParams.deviceTemperature"
          placeholder="请输入设备温度"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="数据上报时间" prop="reportTime">
        <el-date-picker clearable
          v-model="queryParams.reportTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择数据上报时间">
        </el-date-picker>
      </el-form-item>
     
      <el-form-item>
        <el-button type="primary" icon="search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['yanjing:point:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['yanjing:point:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['yanjing:point:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['yanjing:point:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="pointList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="监测点位编码" align="center" prop="pointCode" />
      <el-table-column label="监测点位名称" align="center" prop="pointName" />
      <el-table-column label="监测点位设备名称" align="center" prop="pointDeviceName" />
      <el-table-column label="相位" align="center" prop="phase" />
      <el-table-column label="所属杆号" align="center" prop="poleCode" />
      <el-table-column label="所属线路编码" align="center" prop="lineCode" />
      <el-table-column label="所属线路名称" align="center" prop="lineName" />
      <el-table-column label="告警状态" align="center" prop="pointAlarmStatus" :formatter="formatterTemperatureStatus" />
      <el-table-column label="设备温度" align="center" prop="deviceTemperature" >
        <template #default="scope">
          {{ scope.row.deviceTemperature }} °C
        </template>
      </el-table-column>
      <el-table-column label="设备电压" align="center" prop="deviceVoltage" >
        <template #default="scope">
          {{ scope.row.deviceVoltage }} V
        </template>
      </el-table-column>
      <!-- <el-table-column label="设备状态" align="center" prop="deviceStatus" /> -->
      <el-table-column label="告警时间" align="center" prop="reportTime" width="180" />


    
      <el-table-column label="操作" align="center" width="300%" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button
            size="mini"
            type="text"
            icon="edit"
            @click="selecthistorylist(scope.row)"
            v-hasPermi="['yanjing:history:selecthistorylist']"
          >历史告警</el-button>
          <el-button
            size="mini"
            type="text"
            icon="edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['yanjing:point:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['yanjing:point:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page.sync="queryParams.pageNum"
      v-model:limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

        <!-- 历史告警信息对话框对话框 -->
    <el-dialog :title="selecthistorylisttitle" v-model="selecthistorylistopen" width="1100px" append-to-body>
      <el-table v-loading="selecthistorylistloading" :data="selecthistorylisttable" >
          <el-table-column type="selection" width="55" align="center" />
          <el-table-column label="监测点位编码" align="center" prop="pointCode" />
          <el-table-column label="监测点位名称" align="center" prop="pointName" />
          <el-table-column label="监测点位设备名称" align="center" prop="pointDeviceName" />
          <el-table-column label="相位" align="center" prop="phase" />
          <el-table-column label="告警状态" align="center" prop="pointAlarmStatus" :formatter="formatterTemperatureStatus" />
          <el-table-column label="设备温度" align="center" prop="deviceTemperature" >
            <template #default="scope">
              {{ scope.row.deviceTemperature }} °C
            </template>
          </el-table-column>
          <el-table-column label="设备电压" align="center" prop="deviceVoltage" >
            <template #default="scope">
              {{ scope.row.deviceVoltage }} V
            </template>
          </el-table-column>

          <!-- <el-table-column label="设备状态" align="center" prop="deviceStatus" /> -->
          <el-table-column label="告警时间" align="center" prop="reportTime" width="180" />
      </el-table>
      <pagination
        v-show="selecthistorylisttotal>0"
        :total="selecthistorylisttotal"
        v-model:page.sync="queryHistoryParams.pageNum"
        v-model:limit.sync="queryHistoryParams.pageSize"
        @pagination="selecthistorylistdialog"
      />
      <div>
        <br>
        <br>
      </div>
      <div align="center" slot="footer" class="dialog-footer">
        <el-button  @click="selecthistorylistopen=false">返  回</el-button>
      </div>
    </el-dialog>

    <!-- 添加或修改监测点位信息对话框 -->
    <el-dialog :title="title" v-model="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="140px">
        <el-form-item label="监测点位编码" prop="pointCode">
          <el-input v-model="form.pointCode" placeholder="请输入点位名称" />
        </el-form-item>
        <el-form-item label="监测点位名称" prop="pointName">
          <el-input v-model="form.pointName" placeholder="请输入点位名称" />
        </el-form-item>
        <el-form-item label="监测点位设备名称" prop="pointDeviceName">
          <el-input v-model="form.pointDeviceName" placeholder="请输入点位设备名称" />
        </el-form-item>
        <el-form-item label="相位" prop="phase">
          <el-input v-model="form.phase" placeholder="请输入相位" />
        </el-form-item>
          <el-form-item label="线路" prop="lineId">
              <el-select
                v-model="form.lineId"
                placeholder="请选择线路"
                clearable
              >
                <el-option
                  v-for="lineOne in lineList"
                  :key="lineOne.id"
                  :label="lineOne.lineName"
                  :value="lineOne.id"
                ></el-option>
              </el-select>
            </el-form-item>
          <el-form-item label="电杆" prop="poleId">
              <el-select
                v-model="form.poleId"
                placeholder="请选择线路"
                clearable
              >
                <el-option
                  v-for="poleOne in poleList"
                  :key="poleOne.id"
                  :label="poleOne.poleName"
                  :value="poleOne.id"
                ></el-option>
              </el-select>
            </el-form-item>


       
      
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listPoint, getPoint, delPoint, addPoint, updatePoint,selectMonitorPointAndLinePoleInfoList } from "@/api/line/point.js";
import { selectLineAndAreaInfoList } from "@/api/line/line.js";
import { selectElectricityPoleAndLineInfoList } from "@/api/line/pole.js";
import { listHistory } from "@/api/line/history.js";

export default {
  name: "Point",
  data() {
    return {
      historyPointId:null,
      selecthistorylisttable:[],
      selecthistorylistopen:null,
      selecthistorylistloading:null,
      selecthistorylisttitle:null,
      lineList:[],
      poleList:[],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 监测点位信息表格数据
      pointList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        pointName: null,
        pointDeviceName: null,
        phase: null,
        poleId: null,
        lineId: null,
        pointAlarmStatus: null,
        deviceTemperature: null,
        deviceStatus: null,
        reportTime: null,
        standby1: null,
        standby2: null,
        standb3: null,
        createEmployeeId: null,
        updateEmployeeId: null
      },
        queryHistoryParams: {
        pageNum: 1,
        pageSize: 5,
        pointName: null,
        pointDeviceName: null,
        phase: null,
        poleId: null,
        lineId: null,
        pointAlarmStatus: null,
        deviceTemperature: null,
        deviceStatus: null,
        reportTime: null,
        standby1: null,
        standby2: null,
        standb3: null,
        createEmployeeId: null,
        updateEmployeeId: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
    this.selectLineAndAreaInfoList();
    this.selectElectricityPoleAndLineInfoList();
  },
  methods: {
    selecthistorylist(row) {
      this.historyPointId = null;
      this.selecthistorylistloading = false;
      this.queryHistoryParams.pointId = row.id;
      this.historyPointId = row.id;
      listHistory(this.queryHistoryParams).then(response => {
        this.selecthistorylisttable = response.rows;
        this.selecthistorylistopen = true;
        this.selecthistorylisttitle = "历史告警信息";
        this.selecthistorylisttotal = response.total;
        this.selecthistorylistloading = false;
      });
    },
    selecthistorylistdialog() {
      this.selecthistorylistloading = false;
       this.queryHistoryParams.pointId = this.historyPointId;
      listHistory(this.queryHistoryParams).then(response => {
        this.selecthistorylisttable = response.rows;
        this.selecthistorylistopen = true;
        this.selecthistorylisttitle = "历史告警信息";
        this.selecthistorylisttotal = response.total;
        this.selecthistorylistloading = false;
      });
    },
    formatterTemperatureStatus(row, column, cellValue, index) {
      if(row.pointAlarmStatus != null && row.pointAlarmStatus == '0'){
        return "温度异常";
      }else{
        return "温度正常";
      }
    },
    selectLineAndAreaInfoList() {
      selectLineAndAreaInfoList(this.queryParams).then(response => {
        this.lineList = response.rows;
      });
    },
    selectElectricityPoleAndLineInfoList() {
      selectElectricityPoleAndLineInfoList(this.queryParams).then(response => {
        this.poleList = response.rows;
      });
    },
    /** 查询监测点位信息列表 */
    getList() {
      this.loading = true;
      selectMonitorPointAndLinePoleInfoList(this.queryParams).then(response => {
        this.pointList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        pointName: null,
        pointDeviceName: null,
        phase: null,
        poleId: null,
        lineId: null,
        pointAlarmStatus: null,
        deviceTemperature: null,
        deviceStatus: null,
        reportTime: null,
        standby1: null,
        standby2: null,
        standb3: null,
        createTime: null,
        createEmployeeId: null,
        updateTime: null,
        updateEmployeeId: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加监测点位信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getPoint(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改监测点位信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updatePoint(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addPoint(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除监测点位信息编号为"' + ids + '"的数据项？').then(function() {
        return delPoint(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('yanjing/point/export', {
        ...this.queryParams
      }, `point_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
